<?php


namespace app\admin\model;


use think\Db;
use think\Model;

class News extends Model
{
    protected $table = 'xm_news';

    public static function getListInfo($page, $limit, $data)
    {
        $where = [];
        //活动名称搜索
        if (!empty($data['title'])) {
            $where['title'] = ['like', '%' . rtrim($data['title']) . '%'];
        }
        if (isset($data['status'])) {
            switch ($data['status']) {
                case 0:
                    $where['start_time'] = ['>', time()];
                    break;
                case 1:
                    $where['start_time'] = ['<=', time()];
                    $where['end_time'] = ['>=', time()];
                    break;
                case 2:
                    $where['end_time'] = ['<', time()];
            }
        }
        //活动开始时间
        if (!empty($data['start_time'])) {
            $where['start_time'] = ['>=', strtotime(rtrim($data['start_time']))];
        }
        //活动结束时间
        if (!empty($data['end_time'])) {
            $where['end_time'] = ['<=', strtotime(rtrim($data['end_time']) . " 23:59:59")];
        }
        //分页
        $start = ($page - 1) * $limit;
        $count = Db::name('news')->where($where)->count();
        $list = Db::name('news')->where($where)->limit($start, $limit)->order('order', 'asc')->select();
        $regionList = $adminInfo = $info = [];
        foreach ($list as $key => $value) {
            $list[$key]['start_time'] = !empty($value['start_time']) ? date("Y-m-d", $value['start_time']) : '';//活动开始时间
            $list[$key]['end_time'] = !empty($value['end_time']) ? date("Y-m-d", $value['end_time']) : '';   //活动结束时间
            $list[$key]['create_time'] = !empty($value['create_time']) ? date("Y-m-d H:i:s", $value['create_time']) : '';   //活动创建时间
            if ($value['start_time'] > time()) {
                $list[$key]['status'] = "未开始";
            } elseif (($value['start_time'] <= time()) && ($value['end_time'] >= time())) {
                $list[$key]['status'] = "进行中";
            } else {
                $list[$key]['status'] = "已结束";
            }
            //城市
            if (!empty($value['citys'])) {
                $ci=json_decode($value['citys'],true);
                $ci=$ci[2];
                $regionList = Db::name('region')->where(['region_id' => ['in', $ci]])->field('region_name')->select();
                $region_name = '';
                foreach ($regionList as $vml) {
                    $region_name .= $vml['region_name'] . ',';
                }
                $list[$key]['region_name'] = rtrim($region_name, ',');
            } else {
                $list[$key]['region_name'] = '全国';
            }
            //创建人
            $adminInfo = getAdminInfo($value['admin_id']);
            $list[$key]['admin_name'] = $adminInfo['user_name'];

        }
        return array(
            'code' => 0,
            'msg' => '',
            'count' => $count,
            'data' => $list
        );
    }
}